package com.example.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.gulimall.product.entity.BrandEntity;
import com.example.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gulimall.common.utils.PageUtils;
import com.example.gulimall.common.utils.Query;

import com.example.gulimall.product.dao.CategoryBrandRelationDao;
import com.example.gulimall.product.entity.CategoryBrandRelationEntity;
import com.example.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
@Autowired
private BrandServiceImpl brandService;
@Autowired
private CategoryServiceImpl categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //保存品牌详细名称以及分类名称
        Long brandId = categoryBrandRelation.getBrandId();
        categoryBrandRelation.setBrandName(brandService.getById(brandId).getName());
        Long catelogId = categoryBrandRelation.getCatelogId();
        categoryBrandRelation.setCatelogName(categoryService.getById(catelogId).getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(BrandEntity brand) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brand.getBrandId());
        categoryBrandRelationEntity.setBrandName(brand.getName());
        this.update(categoryBrandRelationEntity,new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getBrandId,categoryBrandRelationEntity.getBrandId()));

    }

    @Override
    public void updateCategory(CategoryEntity category) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(category.getCatId());
        categoryBrandRelationEntity.setCatelogName(category.getName());
        this.update(categoryBrandRelationEntity,new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId,categoryBrandRelationEntity.getCatelogId()));


    }

    @Override
    public List<BrandEntity> getRelationBrandByCatId(Long catId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> categoryBrandRelationEntityLambdaQueryWrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList= this.list(categoryBrandRelationEntityLambdaQueryWrapper);
        List<BrandEntity> brandEntityList = categoryBrandRelationEntityList.stream().map((categoryBrandRelationEntity -> {
            BrandEntity brand = brandService.getById(categoryBrandRelationEntity.getBrandId());
            return brand;
        })).collect(Collectors.toList());
        return brandEntityList;
    }


}
